package com.exhibition.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.exhibition.service.ITbAdminService;
import com.exhibition.service.ITbHostService;
import com.exhibition.service.ITbUnitService;
import com.exhibition.util.Util;

@Controller
public class TbAdminController {
	
	@Autowired
	ITbAdminService tbAdminService;
	@Autowired
	ITbHostService tbHostService;
	@Autowired
	ITbUnitService tbUnitService;
	
	@RequestMapping("admin/login.do")	
	public ModelAndView insert(String userName,String userPwd,String userRole,HttpServletRequest request){
		ModelAndView mav=new ModelAndView("../login.jsp");
		Map<String,Object> map=new HashMap<String,Object>();
		if(userRole.equals("1")){
			map.put("adm_account", userName);
			map.put("adm_pwd", userPwd);
			Integer count = tbAdminService.getList(map).size();
			if(count>0){
				request.getSession().setAttribute("account", userName);
				mav=new ModelAndView(new RedirectView("./index.jsp"));				
			}else{
				mav.addObject("msg", "用户或密码错误");
			}
		}else if(userRole.equals("2")){
			map.put("host_account", userName);
			map.put("host_pwd", userPwd);
			Integer count = tbHostService.getList(map).size();
			if(count>0){
				request.getSession().setAttribute("account", userName);
				map.put("host_account", userName);
				request.getSession().setAttribute("host_id", tbHostService.getOne(map).get("host_id"));
				mav=new ModelAndView(new RedirectView("../host/index.jsp"));				
			}else{
				mav.addObject("msg", "用户或密码错误");
			}
		}else if(userRole.equals("3")){
			map.put("unit_account", userName);
			map.put("unit_pwd", userPwd);
			Integer count = tbUnitService.getList(map).size();
			if(count>0){
				map.put("unit_account", userName);
				request.getSession().setAttribute("unit_id", tbUnitService.getOne(map).get("unit_id"));
				mav=new ModelAndView(new RedirectView("../unit/index.jsp"));			
			}else{
				mav.addObject("msg", "用户或密码错误");
			}
		}
		return mav;
	}
	/**
	 * 登出
	 * @param request
	 * @return
	 */
	@RequestMapping("admin/logout.do")	
	public ModelAndView logout(HttpServletRequest request){
		ModelAndView mav=new ModelAndView(new RedirectView("../login.jsp"));
		request.getSession().removeAttribute("account");
		return mav;
	}
	
	/**修改个人信息
	 * @param request
	 * @return
	 */
	@RequestMapping("admin/info.do")	
	public ModelAndView info(HttpServletRequest request){
		ModelAndView mav=new ModelAndView("./myinfo/info.jsp");
		Object account=request.getSession().getAttribute("account");
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("adm_account", account);
		mav.addObject("admin", tbAdminService.getOne(map));
		return mav;
	}
	/**
	 * 确认修改信息
	 * @param ad_id
	 * @param ad_name
	 * @param ad_phone
	 * @param ad_email
	 * @param ad_QQ
	 * @param request
	 * @return
	 */
	@RequestMapping("admin/infoOk.do")
	public ModelAndView infoOk(
			String adm_id,
			String adm_account,
			String adm_pwd,
			String adm_name,
			String adm_sex,
			String adm_dep,
			String adm_phone,
			String adm_email,
			HttpServletRequest request){
		ModelAndView mav=new ModelAndView("redirect:/admin/info.do");
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("adm_id", adm_id);
		map.put("adm_account", adm_account);
		map.put("adm_pwd", adm_pwd);
		map.put("adm_name", adm_name);
		map.put("adm_sex", adm_sex);
		map.put("adm_dep", adm_dep);
		map.put("adm_phone", adm_phone);
		map.put("adm_email", adm_email);
		tbAdminService.update(map);
		mav.addObject("msg", "修改成功");
		return mav;
	}
}
